package project.fileAccess;
import project.*;
import java.util.ArrayList;
import java.io.*;
import java.sql.*;


public class ReadLocation {
    public void main (String[] args){
        ArrayList<ConnectionType> connList = new ArrayList<ConnectionType>();
        ConnectionType connTemp = new ConnectionType();
        	try {
         FileInputStream fis = new FileInputStream("locations.dat");
        ObjectInputStream ois = new ObjectInputStream(fis);
    while(true){
        int i =0;
        try{
    connTemp.dbInfo = (DatabaseInfo)ois.readObject();

        connTemp.conn = DriverManager.getConnection("jdbc:mysql://" + connTemp.dbInfo.getHostName()+":"+connTemp.dbInfo.getPortNo()+"/"+connTemp.dbInfo.getDatabaseName(),
		connTemp.dbInfo.getUserName(),connTemp.dbInfo.getPassword());
//		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/numberTrans","root","password");

		} catch (EOFException e){
            break;
        }

    ResultSet values = null;
    ResultSet primary = null;
    ResultSet tables = null;

    try {
        //System.out.println(connTemp.dbInfo.getDatabaseName());
    tables = connTemp.conn.getMetaData().getTables("", "", "", null);
    Statement stmt = connTemp.conn.createStatement();

//    rs = stmt.executeQuery("select * from numbers1");
        while (tables.next()) {

        primary = connTemp.conn.getMetaData().getPrimaryKeys("", "", tables.getString(3));
            while (primary.next()) {
//            System.out.println("Table: " +tables.getString(3) + ", Primary Key: " + primary.getString(4));
            values = stmt.executeQuery("select " + primary.getString(4) + " from " + tables.getString(3));
            while (values.next()){
               // connTemp.primaryValues.add(Integer.parseInt(values.getString(1)));
//                System.out.print(values.getString(1) + " ");
            }
            connList.add(connTemp);
            //System.out.println();
            }
    }
    } catch (SQLException e) {
        System.err.println("Error");
    }
    i++;
    }///
    } catch (SQLException e) {
		System.err.println(e.getMessage());
        System.exit(1);
		} catch (IOException e) {
            System.err.println("File Not Found");
        } catch (ClassNotFoundException e){
            System.err.println("Class Not Found");
        }

    }

}
